package org.one.stone.soup.grfxML;

import org.one.stone.soup.mjdb.data.field.Field;
import org.one.stone.soup.mjdb.data.field.LockException;
  /*
 * Copyright 2000-2001 Wet-Wired.com Ltd.,
 * Portsmouth England
 * All rights reserved.
 */

/**
	*
	* @author Nik Cross
*/
public class GrfxMLStoreProcessor {
public static DataStore initialize(DataStore store)
{
	DataStore newStore = new DataStore(store.getEngine());
	newStore.setHashed(true);
	DataStore refStore = new DataStore(store.getEngine());
	refStore.setHashed(true);

	String name;
	Field field;
	Object fieldOwner;

	Field reference;

	for(int loop=0; loop<store.size();)
	{
		field = store.get(loop);
		fieldOwner = store.getOwner(loop);
		name = field.getId();

		try{
			if(NameHelper.isSystemName(name))
			{
				store.remove(loop,store);
			}
			else if(NameHelper.isReadOnlyName(name))
			{
				field.setId( field.getId().substring(1),store );
				field.lock(store);
				refStore.add( field,fieldOwner );
				store.remove(loop,store);

				if(GrfxMLEngine.DEBUG==true)
				{
					System.out.println("LOCKED REFERENCE ADDED:"+field.getId()+"."+field.getRef());
				}
			}
			else if(field.isReference==true)
			{
				refStore.add( field,fieldOwner );
				store.remove(loop,store);

				if(GrfxMLEngine.DEBUG==true)
				{
					System.out.println("REFERENCE ADDED:"+field.getId()+"."+field.getRef());
				}
			}
			else if(NameHelper.isReferenceName(name))
			{
				field.isReference=true;
				field.setId(field.getId().substring(1),store);
				refStore.add( field,fieldOwner );
				store.remove(loop,store);

				if(GrfxMLEngine.DEBUG==true)
				{
					System.out.println("REFERENCE ADDED:"+field.getId()+"."+field.getRef());
				}
			}
			else
			{
				loop++;
			}
		}
		catch(LockException le){}//dummy catch

	}

	for(int loop=store.size()-1;loop>=0;loop--)
	{
		field = store.get(loop);
		fieldOwner = store.getOwner(loop);
		name = field.getId();

		reference = refStore.findId(name);
		if(reference==null)
		{
			reference = newStore.findId(name);
		}
		else
		{
			if(GrfxMLEngine.DEBUG==true)
			{
				System.out.println("REFERENCE FOUND:"+name+"."+reference.getRef());
			}
		}

		if(reference == null)
		{
			try{
				newStore.add( field,fieldOwner );
				if(GrfxMLEngine.DEBUG==true)
				{
					if(fieldOwner instanceof Field)
					{
						System.out.println("ADDED:"+name+"."+field.getRef()+" from "+((Field)fieldOwner).getId());
					}
					else
					{
						System.out.println("ADDED:"+name+"."+field.getRef()+" from "+fieldOwner);
					}
				}
			}
			catch(LockException le){}//dummy catch
		}
		else
		{// needs Exception handleing for type mismatch
			((ITag)fieldOwner).replace( field,reference,false);
			if(GrfxMLEngine.DEBUG==true)
			{
				System.out.println("REPLACED:"+name+"."+field.getRef()+" with "+reference.getId()+"."+reference.getRef()+" in "+((Field)store.getOwner(loop)).getId());
			}
		}
	}

	for(int loop=0; loop<refStore.size();loop++)
	{
		try{
			newStore.add( refStore.get(loop),refStore.getOwner(loop) );
		}
		catch(LockException le){}//dummy catch
	}

	return newStore;
}
/**
 *
 * @param path java.lang.String
 * @param clone org.one.stone.soup.mjdb.data.field.Field
 */
public static void setCloneName(String path, Field source, Field clone)
{
	if( !NameHelper.isSystemName(source.getId()) )
	{
		try {
			if(NameHelper.isAbsoluteName(source.getId()))
			{
				clone.setId(source.getId().substring(1), source);
			}
			else
			{
				clone.setId(path + "." + source.getId(), source);
			}
		} catch (LockException le) {
		} //Dummy catch
	}

}
}
